package com.sto.stoapp.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;

import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.thread.BillFilterThread;

/**
 * 定时获取拦截订单信息，更新本地订单拦截信息
 */
public class FilterBillService extends Service {

    /**
     * 每4个一小时更新一次数据
     */
    private static final int ONE_HOUR=4*60*60*1000;

    private static final int PENDING_REQUEST=0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //获取网点编号
        String siteCode= StoFilePreference.getInstance(this).getSiteCode();
        //开启获取过滤订单的线程
        new BillFilterThread(siteCode,this).start();
        //End by wangjie登陆成功设置Jpush标签

        //通过AlarmManager的set方法就可以设置一个定时任务
        AlarmManager manager= (AlarmManager) getSystemService(ALARM_SERVICE);
        long triggerAtTime= SystemClock.elapsedRealtime()+ONE_HOUR;
        Intent i=new Intent(this,FilterBillReceive.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this,PENDING_REQUEST,i,PENDING_REQUEST);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pendingIntent);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
